﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using Telerik.Web.UI;

public partial class Admin_UserControl_QL_LoaiSanPham : System.Web.UI.UserControl
{
    DaiCuongMobileTableAdapters.tbCategoriesTableAdapter adtCategory = new DaiCuongMobileTableAdapters.tbCategoriesTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsCallback)
        {
            lblWarning.Visible = false;
            BindToDropDownList(ddlParentCategory);
        }
    }
    protected void btnAddCategory_Click(object sender, EventArgs e)
    {
        //Save new Category
        if (!Page.IsCallback)
        {
            try
            {                
                string CategoryName = txtCategoryName.Text.Trim();
                string CategoryNoSign = ConvertToUnSign(txtCategoryNameNoSign.Text.Trim());               
                if (ddlParentCategory.SelectedValue == "NULL")
                {
                    if (adtCategory.Insert(CategoryName, CategoryNoSign, null) > 0)
                    {
                        lblWarning.Text = "Thực hiện thành công!";
                        lblWarning.CssClass = "Succsess";
                        lblWarning.Visible = true;

                        gvCategory.Rebind();
                    }
                }
                else
                {
                    int ParentCategoryID = Convert.ToInt32(ddlParentCategory.SelectedValue);
                    if (adtCategory.Insert(CategoryName, CategoryNoSign, ParentCategoryID) > 0)
                    {
                        lblWarning.Text = "Thực hiện thành công!";
                        lblWarning.CssClass = "Succsess";
                        lblWarning.Visible = true;

                        gvCategory.Rebind();
                    }
                }
            }
            catch (Exception ex)
            {
                lblWarning.Text = "Thực hiện thất bại!";
                lblWarning.CssClass = "Warning";
                lblWarning.Visible = true;
            }
        }
    }

    private void BindToDropDownList(DropDownList ddlCate)
    {
        DataTable dtCate = adtCategory.GetCategories();
        if (dtCate.Rows.Count > 0)
        {
            foreach (DataRow row in dtCate.Rows)
            {
                if (Convert.IsDBNull(row["ParentCategoryID"]))
                {
                    ListItem item = new ListItem();
                    item.Text = row["CategoryName"].ToString();
                    item.Value = row["CategoryID"].ToString();
                    ddlCate.Items.Add(item);

                    // Tìm chuyên mục con của nó
                    string ParentID = row["CategoryID"].ToString();
                    GetChildNode(ParentID, dtCate, ddlCate, 0);
                }
            }
        }
    }

    private void GetChildNode(string ParentCateID, DataTable dtCateTemp, DropDownList ddlChildCate, int Level)
    {
        string child = "";
        int level = Level + 1;
        for (int i = 0; i < level; i++)
        {
            child += "|--";
        }
        foreach (DataRow row in dtCateTemp.Rows)
        {
            if (row["ParentCategoryID"].ToString() == ParentCateID)
            {
                ListItem childItem = new ListItem();
                childItem.Text = child + " " + row["CategoryName"].ToString();
                childItem.Value = row["CategoryID"].ToString();
                ddlChildCate.Items.Add(childItem);

                // Đệ qui tìm mục con của nó
                string ParentID = row["CategoryID"].ToString();
                GetChildNode(ParentID, dtCateTemp, ddlChildCate, level);
            }
        }
        Level--;

    }

    //Replace Vietnames to Vietnamese UnSign
    public static string ConvertToUnSign(string text)
    {
        for (int i = 32; i < 48; i++)
        {
            text = text.Replace(((char)i).ToString(), " ");
        }
        text = text.Replace(".", "-");
        text = text.Replace(" ", "-");
        text = text.Replace(",", "-");
        text = text.Replace(";", "-");
        text = text.Replace(":", "-");

        Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

        string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
        return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
    }
    protected void txtCategoryName_TextChanged(object sender, EventArgs e)
    {
        txtCategoryNameNoSign.Text = ConvertToUnSign(txtCategoryName.Text.Trim());
    }
    protected void txtEditCategoryName_TextChanged(object sender, EventArgs e)
    {
       
    }
}